package com.dubito.unsplashapp.ui.collection.detail

import androidx.lifecycle.LiveData
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import com.dubito.unsplashapp.domain.photo.PhotoRepository
import com.dubito.unsplashapp.util.livedata.Event
import kotlinx.coroutines.launch
import retrofit2.Response
import com.dubito.unsplashapp.data.collection.model.Collection
import com.dubito.unsplashapp.data.photo.model.Photo
import com.dubito.unsplashapp.domain.Listing

class CollectionDetailViewModel(
    private val photoRepository: PhotoRepository
) : ViewModel() {

    private val _getCollectionResultLiveData = MutableLiveData<Event<Result<Collection>>>()
    val getCollectionResultLiveData: LiveData<Event<Result<Collection>>> = _getCollectionResultLiveData

    private val _updateCollectionResultLiveData = MutableLiveData<Event<Result<Collection>>>()
    val updateCollectionResultLiveData: LiveData<Event<Result<Collection>>> = _updateCollectionResultLiveData

    private val _deleteCollectionResultLiveData = MutableLiveData<Event<Result<Response<Unit>>>>()
    val deleteCollectionResultLiveData: LiveData<Event<Result<Response<Unit>>>> = _deleteCollectionResultLiveData

    private val _collectionLiveData = MutableLiveData<Collection>()
    val collectionLiveData: LiveData<Collection> = _collectionLiveData

    var isCollectionUsedForAutoWallpaper = false

    private val photoListing = MutableLiveData<Listing<Photo>>()

    val photosLiveData = Transformations.switchMap(photoListing) { it.pagedList }
    val networkStateLiveData = Transformations.switchMap(photoListing) { it.networkState }
    val refreshStateLiveData = Transformations.switchMap(photoListing) { it.refreshState }

//    fun getPhotoListing(collectionId: String) {
//        photoListing.postValue(photoRepository.getCollectionPhotos(collectionId, viewModelScope))
//    }
//
//    fun refreshPhotos() = photoListing.value?.refresh?.invoke()
//
//    fun getCollection(collectionId: String) {
//        viewModelScope.launch {
//            val result = collectionRepository.getCollection(collectionId)
//            if (result is Result.Success) {
//                setCollection(result.value)
//            }
//            _getCollectionResultLiveData.postValue(Event(result))
//        }
//    }

    fun setCollection(collection: Collection) = _collectionLiveData.postValue(collection)

//    fun isOwnCollection() = collectionLiveData.value?.user?.username == loginRepository.getUsername()

//    fun isCollectionUsedForAutoWallpaper(id: String) =
//        autoWallpaperRepository.isCollectionUsedForAutoWallpaper(id)

//    fun addCollectionToAutoWallpaper() {
//        viewModelScope.launch {
//            collectionLiveData.value?.let { autoWallpaperRepository.addCollectionToAutoWallpaper(it) }
//        }
//    }

//    fun removeCollectionFromAutoWallpaper() {
//        viewModelScope.launch {
//            collectionLiveData.value?.let { autoWallpaperRepository.removeCollectionFromAutoWallpaper(it) }
//        }
//    }

//    fun updateCollection(
//        title: String,
//        description: String?,
//        private: Boolean?
//    ) {
//        collectionLiveData.value?.id?.let {
//            viewModelScope.launch {
//                val result = collectionRepository.updateCollection(it, title, description, private)
//                if (result is Result.Success) {
//                    val collection = result.value
//                    _collectionLiveData.postValue(collection)
//                }
//                _updateCollectionResultLiveData.postValue(Event(result))
//            }
//        }
//    }
//
//    fun deleteCollection() {
//        collectionLiveData.value?.id?.let {
//            viewModelScope.launch {
//                val result = collectionRepository.deleteCollection(it)
//                _deleteCollectionResultLiveData.postValue(Event(result))
//            }
//        }
//    }
}